/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DataLayer;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Philipp
 */
public class MySqlDb {

    static String userid = "playbox";
    static String password = "playbox";
    static String url = "jdbc:mysql://192.168.0.140:3306/playboxdb";
    Connection conn;

    public MySqlDb() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, userid, password);
        } catch (SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
        } catch (java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException:");
            System.err.println(e.getMessage());
        }
    }

    public ResultSet query(String sql) {
        try {
            java.sql.Statement statement = this.conn.createStatement();
            ResultSet queryResult = statement.executeQuery(sql);

            return queryResult;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    public java.sql.PreparedStatement prepare(String sql) {
        try {
            return this.conn.prepareStatement(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public ResultSet execute(java.sql.PreparedStatement pstatement) {
        try {
            pstatement.execute();
            return pstatement.getResultSet();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    
    public int executeUpdate(java.sql.PreparedStatement pstatement) {
        try {
            return pstatement.executeUpdate();
            //return pstatement.getResultSet();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return -1;
    }
}
